from MsgProcess import MsgProcess, MsgType


class Esp01s(MsgProcess):
    '''万能开关插件'''
    def Text(self, message):

        Data = message['Data']

        #判读是否是8266发过来的消息类型
        if isinstance(Data, dict) and 'type' in dict(Data).keys() and  Data['type'] == 'switch' :
            if Data["state"]==1:
                self.say("OK打开")
            if Data["state"]==0:
                self.say("OK关闭")

        else:
           
            #定义消息类型，发送消息到8266
            if "打开" in Data  or "启动" in Data : 
                self.send(MsgType=MsgType.Text, Receiver="MqttProxy", Data={"type":"switch",'pin':"0","state":1} )
            if "关闭" in Data  or "停止" in Data:
                self.send(MsgType=MsgType.Text, Receiver="MqttProxy", Data={"type":"switch",'pin':"0","state":0} )